package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.mapper.SysDeptMapper;
import com.huike.clues.mapper.SysRoleDeptMapper;
import com.huike.clues.mapper.SysRoleMapper;
import com.huike.clues.service.SysDeptServices;
import com.huike.common.constant.UserConstants;
import com.huike.common.core.domain.MPentity.Dept;
import com.huike.common.core.domain.MPentity.RoleDept;
import com.huike.common.core.domain.TreeSelect;
import com.huike.common.core.domain.entity.SysDept;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description TODO
 * @Author 小不夜侯
 * @Date 2023-08-31 14:15
 */
@Service
public class SysDeptServicesImpl extends ServiceImpl<SysDeptMapper, Dept> implements SysDeptServices {
    @Resource
    private  SysDeptMapper sysDeptMapper;

    /**
     * 获取部门列表
     * @param dept
     * @return
     */
    @Override

    public List<Dept> listDept( Dept dept) {
        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dept::getDelFlag, "0");
            queryWrapper.like( null!= dept.getDeptName(),Dept::getDeptName, dept.getDeptName());
            queryWrapper.eq(null!= dept.getStatus(),Dept::getStatus,dept.getStatus());
        List<Dept> list = sysDeptMapper.selectList(queryWrapper);

        return list;
    }

    /**
     * 新增部门存不存在判断
     *
     * @param sysDept@return
     */
    @Override
    public String notdeptnull(SysDept sysDept) {

        LambdaQueryWrapper<Dept> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(null!= sysDept.getDeptName(),Dept::getDeptName,sysDept.getDeptName());
        Dept dept = sysDeptMapper.selectOne(queryWrapper);
        if (Objects.isNull(dept)){
        return UserConstants.UNIQUE;
        }else if (dept.getDelFlag().equals("0")){
            return UserConstants.UNIQUE;
        }else {
            return UserConstants.NOT_UNIQUE;
        }

    }
    /**
     * 新增部门
     * @param sysDept
     * @return
     */
    @Override
    public void insertDept(SysDept sysDept) {
        Dept dept = new Dept();
    BeanUtils.copyBeanProp(dept,sysDept);
        //创建人修改
        dept.setCreateBy(SecurityUtils.getUsername());
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateBy(SecurityUtils.getUsername());
        dept.setUpdateTime(LocalDateTime.now());
        dept.setDelFlag("0");
        dept.setOrderNum(Integer.parseInt(sysDept.getOrderNum()));
        sysDeptMapper.insert(dept);
    }
    /**
     * 查询部门列表（排除指定部门节点）
     * @param deptId
     * @return
     */
    @Override
    public List<Dept> excludeDeptId(Integer deptId) {
        List<Dept> list = sysDeptMapper.selectList(null);
        for (int i = list.size() - 1; i >= 0; i--) {
            Dept dept = list.get(i);

            if (Objects.equals(dept.getDeptId(), deptId.longValue())){
                  list.remove(i);
            }
        }
        return list;
    }
    /**
     * 修改部门
     * @param sysDept
     * @return
     */
    @Override
    public void updateDept(SysDept sysDept) {
        Dept dept = new Dept();
        BeanUtils.copyBeanProp(dept,sysDept);
        sysDeptMapper.updateById(dept);
    }

}
